﻿using System;
using System.Web.Mvc;
using IndoReputation.Views.ViewModels;
using IndoReputation.Models.BusinessObject;
using System.Web.Security;
using IndoReputation.Models.BusinessLogic;
using IndoReputation.Models.Constants;
using System.Net.Mail;
using System.Text;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {

            var model = new HomeViewModel();
            if (MySession.UserID == 0)
            {
                FormsAuthentication.SignOut();
                Session.Clear();
                return RedirectToAction("Index", "Home");
            }
            ViewBag.Title = "The Indonesia's Global Reputation";
            model.ListChartData = null;
            model.ListChartDataCountry = null;
            model.ListChartDataRegion = null;
            model.ListChartMedia = null;
            model.ListChartMediaCountry = null;
            model.ListChartMediaRegion = null;
            model.ListRegionParent = RegionBOL.GetAllRegionParent();
            model.Test = "!23";
            var currentUser = UsersBOL.GetUserExtendById(MySession.UserID);
            Session["current-user-country-id"] = currentUser.CountryID;
            Session["current-user-region-id"] = currentUser.RegionID;
            return View(model);
        }

        public ActionResult _BuildChart(string dtFrom, string dtTo, int? newsCategory, int? displayBy)
        {
            DateTime dateFrom = DateTime.Now;
            DateTime dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            newsCategory = newsCategory ?? 0;
            displayBy = displayBy ?? 0;
            switch (displayBy)
            {
                case 0:
                    return Json(NewsBOL.GetChartData(dateFrom, dateTo, (int)newsCategory));
                case 1:
                    return Json(NewsBOL.GetChartData(dateFrom, dateTo, (int)newsCategory, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public ActionResult _BuildChartCountry(string dtFrom, string dtTo, int? newsCategory, int? displayBy, int? countryID)
        {
            if (Session["current-user-country-id"] == null)
                countryID = 0;
            else
                countryID = Convert.ToInt32(Session["current-user-country-id"]);
            DateTime dateFrom = DateTime.Now;
            DateTime dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            newsCategory = newsCategory ?? 0;
            displayBy = displayBy ?? 0;            
            if (displayBy == 0)
            {
                return Json(NewsBOL.GetChartDataCountry((int)countryID, dateFrom, dateTo, (int)newsCategory));
            }
            else if (displayBy == 1)
            {
                return Json(NewsBOL.GetChartDataCountry((int)countryID, dateFrom, dateTo, (int)newsCategory, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public ActionResult _BuildChartRegion(string dtFrom, string dtTo, int? newsCategory, int? displayBy, int? regionID)
        {
            /*
            if (Session["current-user-region-id"] == null)
                regionID = 0;
            else
                regionID = Convert.ToInt32(Session["current-user-region-id"]);
             */
            var dateFrom = DateTime.Now;
            var dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            newsCategory = newsCategory ?? 0;
            displayBy = displayBy ?? 0;            
            if (displayBy == 0)
            {
                if (regionID != null)
                    return Json(NewsBOL.GetChartDataRegion((int)regionID, dateFrom, dateTo, (int)newsCategory));
            }
            return displayBy == 1 ? Json(NewsBOL.GetChartDataRegion((int)regionID, dateFrom, dateTo, (int)newsCategory, IRConstants.DisplayBy.Year)) : Json(new { success = false });
        }

        public ActionResult _BuildChartMedia(string dtFrom, string dtTo, int? displayBy)
        {
            var dateFrom = DateTime.Now;
            var dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            displayBy = displayBy ?? 0;
            switch (displayBy)
            {
                case 0:
                    return Json(MediaBOL.GetChartData(dateFrom, dateTo));
                case 1:
                    return Json(MediaBOL.GetChartData(dateFrom, dateTo, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public ActionResult _BuildChartCountryMedia(string dtFrom, string dtTo, int? displayBy, int? countryID)
        {
            countryID = Session["current-user-country-id"] == null ? 0 : Convert.ToInt32(Session["current-user-country-id"]);
            var dateFrom = DateTime.Now;
            var dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            displayBy = displayBy ?? 0;            
            switch (displayBy)
            {
                case 0:
                    return Json(MediaBOL.GetChartDataCountry((int)countryID, dateFrom, dateTo));
                case 1:
                    return Json(MediaBOL.GetChartDataCountry((int)countryID, dateFrom, dateTo, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public ActionResult _BuildChartRegionMedia(string dtFrom, string dtTo, int? displayBy, int? regionID)
        {
            regionID = Session["current-user-region-id"] == null ? 0 : Convert.ToInt32(Session["current-user-region-id"]);
            var dateFrom = DateTime.Now;
            var dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                var dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            displayBy = displayBy ?? 0;
            switch (displayBy)
            {
                case 0:
                    return Json(MediaBOL.GetChartDataRegion((int)regionID, dateFrom, dateTo));
                case 1:
                    return Json(MediaBOL.GetChartDataRegion((int)regionID, dateFrom, dateTo, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public JsonResult _GetTop10News()
        {
            return Json(NewsBOL.GetTopNLastestNews(10));
        }

        [HttpGet]
        public ActionResult TestSendMail()
        {
            return View();
        }
        [HttpPost]
        public ActionResult TestSendMail(int? value)
        {
            /*
            MailMergeSender _mailSender;
            MailMergeMessage _mmm;
            string _outputFolder = System.IO.Path.GetTempPath() + @"\mail";
            _mailSender = new MailMergeSender();            

            _mailSender.LocalHostName = "mail." + Environment.MachineName;
            _mailSender.MaxFailures = 1;
            _mailSender.DelayBetweenMessages = 1000;

            _mailSender.MailOutputDirectory = _outputFolder;
            _mailSender.MessageOutput = MessageOutput.Directory;  // change to MessageOutput.SmtpServer if you like, but be careful :)

            // smtp details - change to your demands
            _mailSender.SmtpHost = "smtp.gmail.com";
            _mailSender.SmtpPort = 587;
            _mailSender.SetSmtpAuthentification("thanhtung2106@gmail.com", "29t50122$$");
            _mailSender.LocalHostName = "antariku.com";
            _mailSender.EnableSsl = false;
            _mmm = new MailMergeMessage("asdsad", "asdasds");


            _mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "support@antariku.com", "Support Indoreputation", System.Text.Encoding.Default));
            _mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "thanhtung2106@gmail.com", "Nguyen Duc Thanh Tung", System.Text.Encoding.Default));
            //_mailSender.SetSmtpAuthentification();
            _mailSender.Send(_mmm);
             */
            var message = new MailMessage {From = new MailAddress("thanhtung2106@gmail.com")};

            message.To.Add(new MailAddress("chumeobeou1987@yahoo.com"));

            message.IsBodyHtml = true;
            message.BodyEncoding = Encoding.UTF8;
            message.Subject = "subject";
            message.Body = "hello receiver";

            var client = new SmtpClient();
            client.Send(message);


            return View();  
        }
    }
}
